#region using

using System;
using System.Web;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Net.Mail;
using System.Threading;
using System.Text.RegularExpressions;
using System.Data;
#endregion

/// <summary>
/// Sends an e-mail to the blog owner whenever a comment is added.
/// Author: Clarence Klopfstein
/// Contact Via: http://www.ckurl.com
/// Adapted from code found here:
/// http://blog.madskristensen.dk/post/BBCode-extension-for-BlogEngineNET-12.aspx
/// 
/// Free to use, would appreciate a note that you are using it and credit being given... 
/// but not required.
/// 
/// Attached ASPX page and following directions in text document would allow you to edit 
/// the provided xml document from the admin section of the site.
/// </summary>
[Extension("Replaces tags with links", "1.2", "<a href=\"http://www.ckurl.com/techblog/page/Quick-Links.aspx\">QL Author</a>")]
public class QuickLinks
{
    static protected ExtensionSettings _settings = null;
	/// <summary>
	/// Hooks up an event handler to the Post.Saving event.
	/// </summary>
	public QuickLinks()
	{
        Post.Saving += new EventHandler<BlogEngine.Core.SavedEventArgs>(Post_Saving);

        SetDefaultSettings();
	}

    protected void SetDefaultSettings()
    {
        ExtensionSettings settings = new ExtensionSettings("QuickLinks");
        settings.AddParameter("TagToReplace", "Tag To Replace", 150, true, true);
        settings.AddParameter("Link", "Link", 150, true);
        settings.AddParameter("CollectClickStats", "CollectClickStats", 5, false);

        settings.AddValue("TagToReplace", "QL");
        settings.AddValue("Link", "http://www.ckurl.com/techblog/page/Quick-Links.aspx");
        settings.AddValue("CollectClickStats", "False");

        settings.IsScalar = false;

        settings.Help = "<strong>Tag to Replace:</strong> Will be the word you will replace with a link. [tag]link[/tag]<br/>" +
            " <strong>Link:</strong> HTTP link to open<br/>" +
            " <strong>Collect Click Stats:</strong> True or False, requires <a href=\"http://blog.madskristensen.dk/post/Count-clicks-on-external-links-in-ASPNET.aspx\"" +
            " target=\"_blank\">this extension</a> to work.<br/>" +
            " <strong>ALL FIELDS ARE REQUIRED - IF YOU ENTER INVALID DATA IT WILL BREAK THE EXTENSION</strong>";

        ExtensionManager.ImportSettings(settings);

        _settings = ExtensionManager.GetSettings("QuickLinks");
    }
	/// <summary>
	/// Function to handle the post.saving event.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void Post_Saving(object sender, EventArgs e)
	{
		//TODO: Make this actually replace some code words with links!
		Post post = (Post)sender;
		string content = post.Content;
        DataTable table = _settings.GetDataTable();
        foreach (DataRow row in table.Rows)
        {
            bool CollectClickStats = Convert.ToBoolean(row["CollectClickStats"].ToString());

            FormatForPost(ref content, (string)row["TagToReplace"], (string)row["Link"], CollectClickStats);
        }
		post.Content = content;
	}

	/// <summary>
	/// FormatForPost - Function adapted from 
	/// http://blog.madskristensen.dk/post/BBCode-extension-for-BlogEngineNET-12.aspx
	/// Changed to make into a link instead of a html tag
	/// </summary>
	/// <param name="content">Body of the post</param>
	/// <param name="rplText">Text to replace</param>
	/// <param name="url">Link to convert text to.</param>
	private void FormatForPost(ref string content, string rplText, string url, bool redirect)
	{
		RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline;
		Regex REGEX = new Regex("\\[" + rplText + "\\](?<inner>(.*?))\\[/" + rplText +
									"\\]", options);

		NestedReplace(ref content, REGEX, "<a href=\"" + url +
											"\" " + (redirect == true ? "onclick=\"trackclick(this)\"" : "") + " target=\"_blank\">${inner}</a>");
	}

	/// <summary>
	/// NestedReplace - Function adapted from
	/// http://blog.madskristensen.dk/post/BBCode-extension-for-BlogEngineNET-12.aspx
	/// Will replace all instances of the text in the post
	/// </summary>
	/// <param name="content">Body of the post</param>
	/// <param name="regexMatch">Regex it is trying to match</param>
	/// <param name="replaceString">URL String to replace with</param>
	private void NestedReplace(ref string content, Regex regexMatch, string replaceString)
	{
		Match m = regexMatch.Match(content);
		while (m.Success)
		{
			string tStr = replaceString.Replace("${inner}", m.Groups["inner"].Value);
			content = content.Substring(0, m.Groups[0].Index) + tStr +
										content.Substring(m.Groups[0].Index +
										m.Groups[0].Length);
			m = regexMatch.Match(content);
		}
	}
}
